import { isNil } from "lodash-es";
const toString = Object.prototype.toString;

export function is(val: unknown, type: string) {
  return toString.call(val) === `[object ${type}]`;
}

export function isDef<T = unknown>(val?: T): val is T {
  return typeof val !== "undefined";
}

export function isUnDef<T = unknown>(val?: T): val is T {
  return !isDef(val);
}

export function isObject(val: any): val is Record<any, any> {
  return val !== null && is(val, "Object");
}

export function isNotEmpty(val: any): boolean {
  return !isNil(val) && !isEmpty(val);
}

/**
 * @Description: 判断对象是否为空
 * @Author: zh
 * @Date: 2021/3/26 13:07
 */
export const isEmptyObject = (data: any): boolean =>
  data === undefined || data === null;

/**
 * @Description: 判断对象是否不为空
 * @Author: zh
 * @Date: 2021/3/26 13:10
 */
export const isNotEmptyObject = (data: any): boolean => !isEmptyObject(data);

export function isEmpty<T = unknown>(val: T): val is T {
  if (isNil(val)) {
    return true;
  }

  if (isArray(val) || isString(val)) {
    return val.length === 0;
  }

  if (val instanceof Map || val instanceof Set) {
    return val.size === 0;
  }

  if (isObject(val)) {
    return Object.keys(val).length === 0;
  }

  return false;
}

export function isDate(val: unknown): val is Date {
  return is(val, "Date");
}

export function isNull(val: unknown): val is null {
  return val === null;
}

export function isNullAndUnDef(val: unknown): val is null | undefined {
  return isUnDef(val) && isNull(val);
}

export function isNullOrUnDef(val: unknown): val is null | undefined {
  return isUnDef(val) || isNull(val);
}

export function isNumber(val: unknown): val is number {
  return is(val, "Number");
}

export function isPromise<T = any>(val: unknown): val is Promise<T> {
  return (
    is(val, "Promise") &&
    isObject(val) &&
    isFunction(val.then) &&
    isFunction(val.catch)
  );
}

export function isString(val: unknown): val is string {
  return is(val, "String");
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export function isFunction(val: unknown): val is Function {
  return typeof val === "function";
}

export function isBoolean(val: unknown): val is boolean {
  return is(val, "Boolean");
}

export function isRegExp(val: unknown): val is RegExp {
  return is(val, "RegExp");
}

export function isArray(val: any): val is Array<any> {
  return val && Array.isArray(val);
}

export function isWindow(val: any): val is Window {
  return typeof window !== "undefined" && is(val, "Window");
}

export function isElement(val: unknown): val is Element {
  return isObject(val) && !!val.tagName;
}

export function isMap(val: unknown): val is Map<any, any> {
  return is(val, "Map");
}

export const isServer = typeof window === "undefined";

export const isClient = !isServer;

export function isUrl(path: string): boolean {
  const reg = /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?/;
  return reg.test(path);
}

//url是否包含某个字段
export function isOp() {
  return window.location.href.includes("/op/");
}

export function isHFunction(fn: any): boolean {
  return toString.call(fn) === "[object Object]";
}

export function isLastIndex(array, index) {
  return index === array.length - 1;
}

/**
 * 判断数组中是否有重复
 * @param arr
 * @returns
 */
export function isRepeat(arr) {
  const hash = {};
  for (const i in arr) {
    if (hash[arr[i]]) {
      return true;
    }
    hash[arr[i]] = true;
  }
  return false;
}

//判断是否空字符串
export const isEmptyString = (value: any): boolean => {
  return (
    value === undefined || value === null || value.toString().trim() === ""
  );
};
